﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CSharpDesign.DAL;
using CSharpDesign.Scripts;
using CSharpDesign.BLL;

namespace CSharpDesign
{
    public partial class Shopping : System.Web.UI.Page
    {
        SqlHelper data = new SqlHelper();
        SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = " 水果商店";
            if (!IsPostBack)
            {
                if (Session["UserName"] == null)
                {
                    Response.Write("<script>alert('您还没有登录，请登录后再使用该功能!');location='javascript:history.go(-1)';</script>");
                }
                else
                {

                    SqlDataReader dr = data.GetDataReader("select top 1 * from GouWuChe where IsCheckout='否' and  Ordeuser='" + Session["UserName"].ToString() + "' order by id desc  ");
                    if (dr.Read())
                    {
                        Label5.Text = dr["Orderid"].ToString();
                    }
                    BinderOrder();

                }
            }
        }
        protected void lbtnCheck_Click(object sender, EventArgs e)
        {
            if (labMoney.Text == "0")
            {
                Alert.AlertAndRedirect("您的购物车中没有任何物品", "Shopping.aspx");


            }
            else
            {


                Response.Redirect("SuccShop.aspx?OrderMember=" + Session["UserName"].ToString());

            }
        }
        protected void lbtnClear_Click(object sender, EventArgs e)
        {
            try
            {
                ShoppingService ss = new ShoppingService();
                string username = Session["UserName"].ToString();
                ss.deleteDingdan(username);
                Response.Redirect("Shopping.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void gvOrderInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrderInfo.PageIndex = e.NewPageIndex;
            gvOrderInfo.DataBind();
        }
        protected void gvOrderInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }
        protected void gvOrderInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                ShoppingService ss = new ShoppingService();
                object RowIndex = gvOrderInfo.DataKeys[e.RowIndex].Value;
                ss.deleteById(RowIndex);
                Response.Redirect("Shopping.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
        }

        protected void gvOrderInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SqlHelper mydata = new SqlHelper();
            string ID = gvOrderInfo.DataKeys[e.RowIndex].Values[0].ToString();
            try
            {
                mydata.RunSql("update DingDan  set shuliang ='" + ((TextBox)gvOrderInfo.Rows[e.RowIndex].FindControl("TextBox1")).Text + "'  where id=" + ID);




                gvOrderInfo.EditIndex = -1;
                BinderOrder();
            }
            catch
            {

            }
        }
        protected void gvOrderInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvOrderInfo.EditIndex = e.NewEditIndex;
            BinderOrder();
        }
        protected void gvOrderInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvOrderInfo.EditIndex = -1;
            BinderOrder();
        }
        public void gvDataBind(GridView gv, string sqlstr)
        {
            sqlconn.Open();
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
            DataSet myds = new DataSet();
            myda.Fill(myds);
            gv.DataSource = myds;
            gv.DataBind();
            sqlconn.Close();
        }
        private void BinderOrder()
        {
            ShoppingService ss = new ShoppingService();
            try
            {
                string username = Convert.ToString(Session["UserName"]);
                string sqlstr = "select * from DingDan where OrderMember='" + username + "' and IsCheckout='否'";
                gvOrderInfo.DataKeyNames = new string[] { "id" };
                gvDataBind(gvOrderInfo, sqlstr);

                SqlHelper data = new SqlHelper();
                SqlDataReader dr1;
                dr1 = data.GetDataReader("select OrderID from DingDan where OrderMember='" + username + "' and IsCheckout='否'");
                string num = ss.GetMoney(username, data, dr1);
                labMoney.Text = num;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}